package com.atguigu.atcrowdfunding.controller;

import com.atguigu.atcrowdfunding.bean.AJAXResult;
import com.atguigu.atcrowdfunding.bean.Page;
import com.atguigu.atcrowdfunding.bean.Role;
import com.atguigu.atcrowdfunding.bean.User;
import com.atguigu.atcrowdfunding.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * lcd  2020/3/31
 * Description:
 */
@Controller
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @RequestMapping("/index")
    public String index(){
        return "role/index";
    }

    @RequestMapping("/add")
    public String add(){
        return "role/add";
    }
    @ResponseBody
    @RequestMapping("/page")
    public Object page(String queryText,Integer pageNo,Integer pageSize){
        AJAXResult ajaxResult = new AJAXResult();
        try {
            Map<String, Object> map = new HashMap<>();
            int offset=(pageNo-1)*pageSize;
            map.put("offset", offset);
            map.put("pagesize", pageSize);
            map.put("queryText", queryText);
            List<Role> roleList=roleService.queryPage(map);
            Integer totalRecord=roleService.queryCount(map);//查询总记录数
            //需要查出总的页码数,
            int totalpage=(totalRecord%pageSize)==0?totalRecord/pageSize:totalRecord/pageSize+1;
            Page<Role> rolePage = new Page<>();
            rolePage.setList(roleList);
            rolePage.setPageNo(pageNo);
            rolePage.setTotalPage(totalpage);
            rolePage.setTotalRecord(totalRecord);
            ajaxResult.setDatas(rolePage);
            ajaxResult.setSuccess(true);
        }catch (Exception e){
            e.printStackTrace();
        }
        return ajaxResult;
    }
    @ResponseBody
    @RequestMapping(value = "/insert")
    public Object insert(Role role){
        AJAXResult ajaxResult = new AJAXResult();
        try {
            roleService.insert(role);
            ajaxResult.setSuccess(true);
        }catch (Exception e){
            e.printStackTrace();
        }
        return ajaxResult;
    }

    @RequestMapping("edit")
    public String edit(String id, Model model) {
        Role role=roleService.findById(id);
        model.addAttribute("role",role);
        return "role/edit";
    }

    @RequestMapping("goAssignPage")
    public String goAssignPage(String id) {
        return "role/assign";
    }

    @ResponseBody
    @RequestMapping(value = "/update")
    public Object update(Role role){
        AJAXResult ajaxResult = new AJAXResult();
        try {
            roleService.update(role);
            ajaxResult.setSuccess(true);
        }catch (Exception e){
            e.printStackTrace();
        }
        return ajaxResult;
    }

    @ResponseBody
    @RequestMapping(value = "/doAssign")
    public Object doAssign(Integer roleid,Integer[] permissionids){
        AJAXResult ajaxResult = new AJAXResult();
        try {
            Map<String, Object> map = new HashMap<>();
            map.put("roleid", roleid);
            map.put("permissionids", permissionids);
            roleService.doAssign(map);
            ajaxResult.setSuccess(true);
        }catch (Exception e){
            e.printStackTrace();
        }
        return ajaxResult;
    }

    @ResponseBody
    @RequestMapping(value = "/delete")
    public Object delete(String id){
        AJAXResult ajaxResult = new AJAXResult();
        try {
            roleService.delete(id);
            ajaxResult.setSuccess(true);
        }catch (Exception e){
            e.printStackTrace();
        }
        return ajaxResult;
    }

    @ResponseBody
    @RequestMapping(value = "/deletes")
    public Object deletess(Integer[] id){
        AJAXResult ajaxResult = new AJAXResult();
        try {
            Map<String, Object> map = new HashMap<>();
            map.put("roleIds", id);
            roleService.deletes(map);
            ajaxResult.setSuccess(true);
        }catch (Exception e){
            e.printStackTrace();
        }
        return ajaxResult;
    }
}
